<?php
if (!defined('XOOPS_ROOT_PATH')) exit();

require_once XOOPS_MODULE_PATH. '/scuttle/class/AbstractListAction.class.php';
require_once XOOPS_MODULE_PATH. '/scuttle/include/TagService.php';
require_once XOOPS_MODULE_PATH. '/scuttle/include/functions.php';
require_once XOOPS_ROOT_PATH . "/core/XCube_PageNavigator.class.php";

class Scuttle_indexAction extends Scuttle_AbstractListAction
{
	var $mConfig = array();
	var $breadcrumbs = null;
	var $ItemArray = array();
	var $moduleHeader = null;
	var $mPagenavi = null;
	var $pageCount = 20;    // page count
	var $getUserId = 0;
	var $confirmMssage = null;
	var $hasEditPermission = false;
	var $isAdmin = false;

	function _getStart()
	{
		return xoops_getrequest('start');
	}

	function _getUid()
	{
		return xoops_getrequest('uid');
	}

	function &_getHandler()
	{
		$handler =& xoops_getmodulehandler('bookmarks');
		return $handler;
	}

	function _getBaseUrl()
	{
		return 'index.php?action=index';
	}

	function &_getPageNavi()
	{
		$navi =& new XCube_PageNavigator($this->_getBaseUrl(), XCUBE_PAGENAVI_START);
		$navi->setStart($this->_getStart());
		$navi->setPerpage($this->pageCount);
		$navi->setTotalItems($this->totalCount);
		if ($this->getUserId > 0) {
			// add pagenavi parameter
			$navi->addExtra('uid', $this->getUserId);
		}
		return $navi;
	}

	function prepare(&$controller, &$xoopsUser, $moduleConfig)
	{
		parent::prepare(&$controller, $xoopsUser, $moduleConfig);
		$this->mConfig = $moduleConfig;

		if ($controller->mRoot->mContext->mUser->isInRole('Module.scuttle.Admin')) {
			$this->isAdmin = true;
		}

	}

	function getDefaultView(&$controller, &$xoopsUser)
	{
		$root =& XCube_Root::getSingleton();
		// may page user check
		if (is_object($xoopsUser)) {
			$uid = $root->mContext->mXoopsUser->get('uid');
		} else {
			$uid = 0;
		}
		$this->getUserId = $this->_getUid();
		if ($uid == $this->getUserId and $uid > 0) {
			$isUser = true;
		} else {
			$isUser = false;
		}

		$handler =& $this->_getHandler();
		$tagHandler = xoops_getmodulehandler('tags');

		$this->getUserId = $this->_getUid();
		$mCriteria =& new CriteriaCompo();
		if ($isUser) {
			$mCriteria->add(new Criteria('uId', $this->getUserId));
		} else {
			$mCriteria->add(new Criteria('bStatus', 2, '<'));
		}
		$this->totalCount =$handler->getCount($mCriteria);

		if ($this->totalCount > 0) {
			$mCriteria->setStart($this->_getStart());
			$mCriteria->setSort('bModified','DESC');
			$mCriteria->setLimit($this->pageCount);
			$this->mObjects =& $handler->getObjects($mCriteria);
			foreach($this->mObjects as $module) {
				$item_array['bId']          = $module->get('bId');
				$item_array['uId']          = $module->get('uId');
				$item_array['bStatus']      = $module->get('bStatus');
				$item_array['bModified']    = $module->get('bModified');
				$item_array['bTitle']       = $module->get('bTitle');
				$item_array['bAddress']     = $module->get('bAddress');
				$item_array['bDescription'] = $module->get('bDescription');
				// TODO
				// get hatena bookmarks (for japaneses)
				$item_array['hatena']       = addHatenaUsers($module->get('bAddress'));
				$item_array['tag_array']    = $tagHandler->getEntoryTagArray($module->get('bId'));
				$item_array['image']        = $module->get('image');
				$this->ItemArray[] =& $item_array;
				unset($item_array);
			}

			// Page navi
			if ($this->totalCount > $this->pageCount ) {
				$this->mPagenavi =$this->_getPageNavi();
				$this->mPagenavi->fetch();
			}

			$this->mObjects =& $handler->getObjects($mCriteria);
			if ($this->isAdmin) {
				// make jquery-in-place-editor javascript
				$inplaceJavaScript = null;
				foreach($this->mObjects as $module) {
					$inplaceJavaScript.= "$(document).ready(function(){
						$(\".editme". $module->get('bId'). "\").editInPlace({
							url: \"inplace.php?id=". $module->get('bId'). "\",
							params: \"ajax=yes\",
							saving_text:\"". _MD_SC_INPLACE_SAVING. "\",
							save_button:'<input type=\"submit\" class=\"inplace_save\" value=\"". _MD_SC_INPLACE_SAVE. "\"/>',
							cancel_button:'<input type=\"submit\" class=\"inplace_cancel\" value=\"". _MD_SC_INPLACE_CANSEL. "\"/>',
							text_size:\"50\",
						});
					});" ;
				}
			}

		} else {
			$this->confirmMssage =_MD_SC_ERROR_NOSUBMITTED ;
		}

		$this->breadcrumbs[] = array('name' => $root->mContext->mModule->mXoopsModule->getVar('name') , 'url' => XOOPS_MODULE_URL . '/scuttle/' ) ;

		$this->moduleHeader.= '<script type="text/javascript" src="' . XOOPS_MODULE_URL .'/scuttle/js/jquery-1.3.2.min.js"></script>'."\n";
		$this->moduleHeader.= '<script type="text/javascript" src="' . XOOPS_MODULE_URL .'/scuttle/js/jquery.prettyPhoto.js"></script>'."\n";

		if ($this->isAdmin) {
			$this->moduleHeader.= '<script type="text/javascript" src="' . XOOPS_MODULE_URL .'/scuttle/js/jsDelete.php"></script>'."\n";
			if ($this->totalCount > 0) {
				// http://code.google.com/p/jquery-in-place-editor/
				$this->moduleHeader.= '<script type="text/javascript" src="' . XOOPS_MODULE_URL . '/scuttle/js/jquery.inplace.pack.js"></script>'."\n";
				$this->moduleHeader.= '<script type="text/javascript">'. $inplaceJavaScript. '</script>'."\n";
			}
		}
		$this->moduleHeader.= '<link rel="stylesheet" type="text/css" href="' . XOOPS_MODULE_URL. '/scuttle/module.css" />'."\n";
		$this->moduleHeader.= '<link rel="stylesheet" type="text/css" media="projection, screen" href="' .  XOOPS_MODULE_URL .'/scuttle/js/css/prettyPhoto.css" />'."\n";
		return SC_FRAME_VIEW_INDEX;
	}

	function executeViewIndex(&$controller, &$xoopsUser, &$render)
	{

		// add xoops_module_header
		$moduleHeader = $render->getAttribute('xoops_module_header'). $this->moduleHeader;
		
		$render->setTemplateName('scuttle_index.html');
		$render->setAttribute('config'  , $this->mConfig);
		$render->setAttribute('isUser'  , is_object($xoopsUser));
		$render->setAttribute('pageNavi'   , $this->mPagenavi);
		$render->setAttribute('item_array'  , $this->ItemArray);
		$render->setAttribute('pretty_photo'  , true);
		if (is_object($xoopsUser)) {
			$render->setAttribute('rangeOptions', array("0" => _MD_SC_BOOKMARKS_RANGE_0, "1" => _MD_SC_BOOKMARKS_RANGE_1, "2" => _MD_SC_BOOKMARKS_RANGE_2));
		} else {
			$render->setAttribute('rangeOptions', array("0" => _MD_SC_BOOKMARKS_GUESTRANGE_0, "1" => _MD_SC_BOOKMARKS_GUESTRANGE_1));
		}
		$render->setAttribute('confirm_mssage', $this->confirmMssage);
		$render->setAttribute('xoops_breadcrumbs'  ,$this->breadcrumbs) ;
		$render->setAttribute('xoops_module_header',$this->moduleHeader);
	}
}
?>